<template>
    <div class="app-container">
    <h1 style="text-align: center;">发布新文章</h1>
        <el-form label-width="120px">
            <el-form-item label="文章标题">
                <el-input v-model="articleInfo.title" placeholder=" 请输入文章标题"/>
            </el-form-item>

            <el-form-item label="文章简述">
                <el-input v-model="articleInfo.simple" placeholder=" 请输入文章简述"/>
            </el-form-item>

            <el-form-item label="文章类别">
                <el-select
                v-model="articleInfo.type"
                placeholder="请选择">
                    <el-option :value="1" label="资讯" />
                    <el-option :value="2" label="公告" />
                </el-select>
            </el-form-item>

            <el-form-item label="文章排序">
            <el-input-number :min="0" v-model="articleInfo.sort" controls-position="right" placeholder="请填写文章排序"/>
            </el-form-item>
            <!-- 文章简介-->
            <el-form-item label="文章简介">
                <tinymce :height="300" v-model="articleInfo.description" />
            </el-form-item>
        <!-- 文章封面 TODO -->
            <!-- 文章封面-->
            <el-form-item label="文章封面">
                <el-upload
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload"
                    :action="BASE_API+'/eduoss/fileoss'"
                    class="avatar-uploader">
                    <img :src="articleInfo.cover" style="width:260px;height:260px" title="点击更换">
                </el-upload>
            </el-form-item>

            <el-form-item>
            <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">
                {{ this.$route.params && this.$route.params.id?'修改':'添加' }}
            </el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
    <script> 
    import article from "@/api/cms/article";
    import Tinymce from '@/components/Tinymce'
    export default {
    components: { Tinymce },
    data() {
        return {
            saveBtnDisabled: false, // 保存按钮是否禁用
            articleInfo:{
                title: '',//文章标题
                type:null,//文章类型
                simple: '',//文章简述
                sort: 0,//排序
                description: '',
                cover: '/static/R-E.jpg',//文章封面
            },
            BASE_API: process.env.BASE_API, // 接口API地址
            articleId:''
        }
     },
    created() {
        this.init()
    },
    methods: {
        init(){
            //获取路由中的id
        if(this.$route.params && this.$route.params.id){
            this.articleId = this.$route.params.id
            //调用 getInfo()
            this.getInfo()
        }else{
            this.articleInfo={
                title: '',//文章标题
                type:null,//文章类型
                simple: '',//文章简述
                sort: 0,//排序
                description: '',
                cover: '/static/R-E.jpg',//文章封面
            }
        }
        },
        //根据文章id查询文章基本信息
        getInfo(){
            article.getArticleInfo(this.articleId)
            .then(response =>{
                console.log(response)
                this.articleInfo = response.data.article
                console.log(this.articleInfo)
            })
        },
        //上传封面成功
        handleAvatarSuccess(res, file){
            this.articleInfo.cover = res.data.url
        },
        //上传封面之前
        beforeAvatarUpload(file){
            const isJPG = file.type === 'image/jpeg'
            const isLt2M = file.size / 1024 / 1024 < 2
            if (!isJPG) {
            this.$message.error('上传头像图片只能是 JPG 格式!')
            }
            if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!')
            }
            return isJPG && isLt2M
        },
        //添加文章
        addarticle(){
            article.addArticle(this.articleInfo)
            .then(response =>{
                // 提示
                this.$message({
                    type: 'success',
                    message: '添加文章信息成功!'
                })
                this.$router.push({ path: '/article/table' })
            })
        },
        updatearticle(){
            article.updateArticleInfo(this.articleInfo)
            .then(response =>{
                this.$message({
                    type: 'success',
                    message: '修改文章信息成功!'
                })
                this.$router.push({ path: '/article/table' })
            })
        },
        //保存并下一步按钮点击回调
        saveOrUpdate() {
            //判断添加还是修改
            if(!this.articleId){
                this.addarticle()
            }else{
                this.updatearticle()
            }
            
        }
    },
    watch: {
            $route(to, from) { //监听路由变化方式
                this.init()
            }
        },
}
</script>
<style scoped>
    .yuan{
        font-size: 16px;
        font-weight: 600;
    }
    .tinymce-container {
        line-height: 29px;
    }
</style>